#/bin/bash

install_nfs-server(){
  kubectl apply -f storageclass/nfs-server/nfs-server.yaml || { echo "Failed to install NFS server"; return 1; }
}

uninstall_nfs-server(){
  kubectl delete -f storageclass/nfs-server/nfs-server.yaml
}

install_nfs-sc(){
  kubectl apply -f storageclass/csi-driver-nfs/v4.4.0/rbac-csi-nfs.yaml || { echo "Failed to install RBAC"; return 1; }
  kubectl apply -f storageclass/csi-driver-nfs/v4.4.0/csi-nfs-driverinfo.yaml || { echo "Failed to install CSI driver info"; return 1; }
  kubectl apply -f storageclass/csi-driver-nfs/v4.4.0/csi-nfs-controller.yaml  || { echo "Failed to install CSI controller"; return 1; }
  kubectl apply -f storageclass/csi-driver-nfs/v4.4.0/csi-nfs-node.yaml  || { echo "Failed to install CSI node"; return 1; }
  kubectl apply -f storageclass/storeclass-csi-driver-nfs/storeclass-csi-driver-nfs.yaml || { echo "Failed to install NFS StorageClass"; return 1; }
}

uninstall_nfs-sc(){
  kubectl delete -f storageclass/csi-driver-nfs/v4.4.0/rbac-csi-nfs.yaml 
  kubectl delete -f storageclass/csi-driver-nfs/v4.4.0/csi-nfs-driverinfo.yaml
  kubectl delete -f storageclass/csi-driver-nfs/v4.4.0/csi-nfs-controller.yaml
  kubectl delete -f storageclass/csi-driver-nfs/v4.4.0/csi-nfs-node.yaml
  kubectl delete -f storageclass/storeclass-csi-driver-nfs/storeclass-csi-driver-nfs.yaml
}


install_ingress-nginx(){
  kubectl apply -f ingress-nginx/ingress-nginx-controller/deploy-ingress-nginx-controller.yaml
}

uninstall_ingress-nginx(){
  kubectl delete -f ingress-nginx/ingress-nginx-controller/deploy-ingress-nginx-controller.yaml
}

install_mysql-deploy(){
  kubectl apply -f mysql/deployment-mysql-v5.7/mysql-ns/mysql-ns.yaml
  kubectl apply -f mysql/deployment-mysql-v5.7/mysql-secret/mysql-secret.yaml
  kubectl apply -f mysql/deployment-mysql-v5.7/mysql-pvc-nfs-csi-dynamic/mysql-pvc-nfs-csi-dynamic.yaml
  kubectl apply -f mysql/deployment-mysql-v5.7/mysql-deployment/mysql-deployment.yaml
}

uninstall_mysql-deploy(){
  kubectl delete -f mysql/deployment-mysql-v5.7/mysql-ns/mysql-ns.yaml
  kubectl delete -f mysql/deployment-mysql-v5.7/mysql-secret/mysql-secret.yaml
  kubectl delete -f mysql/deployment-mysql-v5.7/mysql-pvc-nfs-csi-dynamic/mysql-pvc-nfs-csi-dynamic.yaml
  kubectl delete -f mysql/deployment-mysql-v5.7/mysql-deployment/mysql-deployment.yaml
}


install_mysql-sts(){ #暂未使用
  kubectl apply -f mysql/statefulset-mysql/mysql-ns/mysql-ns.yaml
  kubectl apply -f mysql/statefulset-mysql/mysql-configmap/mysql-configmap.yaml
  kubectl apply -f mysql/statefulset-mysql/mysql-service/mysql-services.yaml
  kubectl apply -f mysql/statefulset-mysql/mysql-statefulset/mysql-statefulset.yaml
}

install_web-tomcat-jpress(){
  kubectl apply -f web/web-ns/web-ns.yaml
  kubectl apply -f web/tomcat-jpress/mysql-secret/mysql-secret.yaml
  kubectl apply -f web/tomcat-jpress/tomcat-jpress-pvc-nfs-csi-dynamic/tomcat-jpress-pvc-nfs-csi-dynamic.yaml
  kubectl apply -f web/tomcat-jpress/tomcat-jpress-deployment/tomcat-jpress-deployment.yaml
}

uninstall_web-tomcat-jpress(){
  kubectl delete -f web/web-ns/web-ns.yaml
  kubectl delete -f web/tomcat-jpress/mysql-secret/mysql-secret.yaml
  kubectl delete -f web/tomcat-jpress/tomcat-jpress-pvc-nfs-csi-dynamic/tomcat-jpress-pvc-nfs-csi-dynamic.yaml
  kubectl delete -f web/tomcat-jpress/tomcat-jpress-deployment/tomcat-jpress-deployment.yaml
}

install_monitoring-ns(){
  kubectl apply -f monitoring/monitoring-ns/monitoring-ns.yaml
}

uninstall_monitoring-ns(){
  kubectl delete -f monitoring/monitoring-ns/monitoring-ns.yaml
}


install_prometheus-server(){
  kubectl apply -f monitoring/prometheus-server/prometheus-serviceaccount.yaml
  kubectl apply -f monitoring/prometheus-server/prometheus-pvc-nfs-csi-dynamic.yaml
  kubectl apply -f monitoring/prometheus-server/prometheus-cfg.yaml
  kubectl apply -f monitoring/prometheus-server/prometheus-alert-rules.yaml
  kubectl apply -f monitoring/prometheus-server/prometheus-deployment.yaml
}

uninstall_prometheus-server(){
  kubectl delete -f monitoring/prometheus-server/prometheus-serviceaccount.yaml
  kubectl delete -f monitoring/prometheus-server/prometheus-pvc-nfs-csi-dynamic.yaml
  kubectl delete -f monitoring/prometheus-server/prometheus-cfg.yaml
  kubectl delete -f monitoring/prometheus-server/prometheus-alert-rules.yaml
  kubectl delete -f monitoring/prometheus-server/prometheus-deployment.yaml
}

install_cadvisor(){
  kubectl apply -f monitoring/cadvisor/cadvisor-ds.yaml
}

uninstall_cadvisor(){
  kubectl delete -f monitoring/cadvisor/cadvisor-ds.yaml
}

install_node-exporter(){
  kubectl apply -f monitoring/node-exporter/node-exporter-ds.yaml
}

uninstall_node-exporter(){
  kubectl delete -f monitoring/node-exporter/node-exporter-ds.yaml
}

#install_metrics-server(){
#  kubectl apply -f metrics-server/metrics-server-deploy.yaml
#}

#uninstall_metrics-server(){
#  kubectl delete -f metrics-server/metrics-server-deploy.yaml
#}

install_kube-state-metrics(){
  kubectl apply -f monitoring/kube-state-metrics/kube-state-metrics-deploy.yaml
}

uninstall_kube-state-metrics(){
  kubectl delete -f monitoring/kube-state-metrics/kube-state-metrics-deploy.yaml
}

install_alertmanager(){
  kubectl apply -f monitoring/alertmanager/alertmanager-message-template-configmap.yaml
  kubectl apply -f monitoring/alertmanager/alertmanager-configmap.yaml
  kubectl apply -f monitoring/alertmanager/alertmanager-deployment.yaml
}

uninstall_alertmanager(){
  kubectl delete -f monitoring/alertmanager/alertmanager-message-template-configmap.yaml
  kubectl delete -f monitoring/alertmanager/alertmanager-configmap.yaml
  kubectl delete -f monitoring/alertmanager/alertmanager-deployment.yaml
}


install_grafana(){
  kubectl apply -f monitoring/grafana/grafana-pvc-nfs-csi-dynamic.yaml
  kubectl apply -f monitoring/grafana/grafana-deployment.yaml
}

uninstall_grafana(){
  kubectl delete -f monitoring/grafana/grafana-pvc-nfs-csi-dynamic.yaml
  kubectl delete -f monitoring/grafana/grafana-deployment.yaml
}

install_zookeeper(){
  kubectl apply -f plug/plug-ns.yaml
  kubectl apply -f plug/zookeeper/zk-pvc-nfs-csi-dynamic.yaml
  kubectl apply -f plug/zookeeper/zk-deployment.yaml
}

uninstall_zookeeper(){
  kubectl delete -f plug/plug-ns.yaml
  kubectl delete -f plug/zookeeper/zk-pvc-nfs-csi-dynamic.yaml
  kubectl delete -f plug/zookeeper/zk-deployment.yaml
}

install_kafka(){
  kubectl apply -f plug/kafka/kafka-pvc-nfs-csi-dynamic.yaml
  kubectl apply -f plug/kafka/kafka-deployment.yaml
}

uninstall_kafka(){
  kubectl delete -f plug/kafka/kafka-pvc-nfs-csi-dynamic.yaml
  kubectl delete -f plug/kafka/kafka-deployment.yaml
}

install_es(){
  kubectl apply -f elk/elk-ns/elk-ns.yaml
  kubectl apply -f elk/elasticsearch/es-pvc-nfs-csi-dynamic/es-pvc-nfs-csi-dynamic.yaml
  kubectl apply -f elk/elasticsearch/es-deployment/es-deployment.yaml
}

uninstall_es(){
  kubectl delete -f elk/elk-ns/elk-ns.yaml
  kubectl delete -f elk/elasticsearch/es-pvc-nfs-csi-dynamic/es-pvc-nfs-csi-dynamic.yaml
  kubectl delete -f elk/elasticsearch/es-deployment/es-deployment.yaml
}

install_kibana(){
  kubectl apply -f elk/kibana/kibana-pvc-nfs-csi-dynamic.yaml
  kubectl apply -f elk/kibana/kibana-deployment.yaml
}

uninstall_kibana(){
  kubectl delete -f elk/kibana/kibana-pvc-nfs-csi-dynamic.yaml
  kubectl delete -f elk/kibana/kibana-deployment.yaml
}

install_filebeat(){
  kubectl apply -f elk/filebeat/filebeat-daemonset/filebeat-service-account.yaml
  kubectl apply -f elk/filebeat/filebeat-daemonset/filebeat-configmap.yaml
  kubectl apply -f elk/filebeat/filebeat-daemonset/filebeat-daemonset.yaml
}

uninstall_filebeat(){
  kubectl delete -f elk/filebeat/filebeat-daemonset/filebeat-service-account.yaml
  kubectl delete -f elk/filebeat/filebeat-daemonset/filebeat-configmap.yaml
  kubectl delete -f elk/filebeat/filebeat-daemonset/filebeat-daemonset.yaml
}

install_logstash(){
  kubectl apply -f elk/logstash/logstash-pipeline-configmap.yaml
  kubectl apply -f elk/logstash/logstash-deployment.yaml
}

uninstall_logstash(){
  kubectl delete -f elk/logstash/logstash-pipeline-configmap.yaml
  kubectl delete -f elk/logstash/logstash-deployment.yaml
}

install_jenkins(){
  kubectl apply -f jenkins/01-namespace-jenkins.yaml
  kubectl apply -f jenkins/02-pvc-jenkins.yaml
  kubectl apply -f jenkins/03-rbac-jenkins.yaml
  kubectl apply -f jenkins/04-deploy-jenkins.yaml
  kubectl apply -f jenkins/05-service-jenkins.yaml
}

uninstall_jenkins(){
  kubectl delete -f jenkins/01-namespace-jenkins.yaml
  kubectl delete -f jenkins/02-pvc-jenkins.yaml
  kubectl delete -f jenkins/03-rbac-jenkins.yaml
  kubectl delete -f jenkins/04-deploy-jenkins.yaml
  kubectl delete -f jenkins/05-service-jenkins.yaml
}


install_menu(){
  echo "Please select the software you want to install"
  echo "1) Install nfs-server"
  echo "2) Install nfs-sc"
  echo "3) Install ingress-nginx"
  echo "4) Install mysql-deploy"
  echo "5) Install web-tomcat-jpress"
  echo "6) Install monitoring-ns"
  echo "7) Install prometheus-server"
  echo "8) Install cadvisor"
  echo "9) Install node-exporter"
  echo "10) Install kube-state-metrics"
  echo "11) Install alertmanager"
  echo "12) Install grafana"
  echo "13) Install zookeeper"
  echo "14) Install kafka"
  echo "15) Install es"
  echo "16) Install kibana"
  echo "17) Install filebeat"
  echo "18) Install logstash"
  echo "19) Install jenkins"
  echo "0) Exit"
}

uninstall_menu(){
  echo "Please select the software you want to uninstall"
  echo "1) Uninstall nfs-server"
  echo "2) Uninstall nfs-sc"
  echo "3) Uninstall ingress-nginx"
  echo "4) Uninstall mysql-deploy"
  echo "5) Uninstall web-tomcat-jpress"
  echo "6) Uninstall monitoring-ns"
  echo "7) Uninstall prometheus-server"
  echo "8) Uninstall cadvisor"
  echo "9) Uninstall node-exporter"
  echo "10) Uninstall kube-state-metrics"
  echo "11) Uninstall alertmanager"
  echo "12) Uninstall grafana"
  echo "13) Uninstall zookeeper"
  echo "14) Uninstall kafka"
  echo "15) Uninstall es"
  echo "16) Uninstall kibana"
  echo "17) Uninstall filebeat"
  echo "18) Uninstall logstash"
  echo "19) Uninstall jenkins"
  echo "0) Exit"
}


while true; do
  clear
  echo -en "\E[$[RANDOM%7+31];1m"
  echo "1) Installation function"
  echo "2) Uninstallation function" 
  echo "0) Exit" 
  read -p "Please enter installation options [0-2]: " choice
  
  case $choice in
    1)
      while true; do
        install_menu    
        read -p "Please enter installation options [number]: " install_choice
        case $install_choice in
          1) install_nfs-server ;;
          2) install_nfs-sc ;;
          3) install_ingress-nginx ;;
          4) install_mysql-deploy ;;
          5) install_web-tomcat-jpress ;;
          6) install_monitoring-ns ;;
	  7) install_prometheus-server ;;
	  8) install_cadvisor ;;
	  9) install_node-exporter ;;
          10) install_kube-state-metrics ;;
          11) install_alertmanager ;;
          12) install_grafana ;;
          13) install_zookeeper ;;
          14) install_kafka ;;
          15) install_es ;;
          16) install_kibana ;;
          17) install_filebeat ;;
          18) install_logstash ;;
          19) install_jenkins ;;
          0) break ;;
          *) echo "Invalid selection, please select again" ;;
        esac
        read -p "Press any key to return to the installation menu..." -n 1 -s
        clear
      done
      ;;
    2)
      while true; do
        uninstall_menu
        read -p "Please enter installation options [number]: " uninstall_choice
        case $uninstall_choice in
          1) uninstall_nfs-server ;;
          2) uninstall_nfs-sc ;;
          3) uninstall_ingress-nginx ;;
          4) uninstall_mysql-deploy ;;
          5) uninstall_web-tomcat-jpress ;;
	  6) uninstall_monitoring-ns ;;
	  7) uninstall_prometheus-server ;;
	  8) uninstall_cadvisor ;;
	  9) uninstall_node-exporter ;;
	  10) uninstall_kube-state-metrics ;;
          11) uninstall_alertmanager ;;
          12) uninstall_grafana ;;
          13) uninstall_zookeeper ;;
          14) uninstall_kafka ;;
          15) uninstall_es ;;
          16) uninstall_kibana ;;
          17) uninstall_filebeat ;;
          18) uninstall_logstash ;;
          19) uninstall_jenkins ;;
          0) break ;;
          *) echo "Invalid selection, please select again" ;;
        esac
        read -p "Press any key to return to the installation menu..." -n 1 -s
        clear
      done
      ;;
    0)
      echo "Exit the program..."
      exit 0
      ;;
    *)
      echo "Invalid selection, please select again"
      ;;
  esac
  read -p "Press any key to return to the installation menu..." -n 1 -s
  clear
done
